package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.modular.crm.entity.GwakT;
import cn.e4j.modular.crm.entity.GwalT;
import cn.e4j.modular.crm.mapper.GwalTMapper;
import cn.e4j.modular.crm.service.GwalTService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  客户联系人 服务实现类
 * </p>
 *
 * @author miaoyl
 * @since 2019-01-11
 */
@Service
public class GwalTServiceImpl extends ServiceImpl<GwalTMapper, GwalT> implements GwalTService {

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getDetailList(Page<Object> page, Dto dto) {
        List<Map<String, Object>> detailDtos = this.baseMapper.listDetailPage(page, dto);
        return detailDtos;
    }

    @Transactional(readOnly = true)
    public String getMaxGwal002(String ent, String gwal001) {
        String maxGwal002 = this.baseMapper.getMaxGwal002(ent, gwal001);
        return maxGwal002;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwadList(Dto dto) {
        List<Map<String, Object>> gwadDtos = this.baseMapper.getGwadList(dto);
        return gwadDtos;
    }

    @Transactional(readOnly = true)
    public GwalT getByGwal003(String ent, String gwal001) {
        UpdateWrapper<GwalT> gwalTUpdateWrapper = new UpdateWrapper<>();
        gwalTUpdateWrapper.eq("GWALENT",ent);
        gwalTUpdateWrapper.eq("GWAL001",gwal001);
        gwalTUpdateWrapper.eq("GWAL003","Y");
        GwalT gwal = this.baseMapper.selectOne(gwalTUpdateWrapper);
        return gwal;
    }

    @Transactional
    @Override
    public void add(GwalT gwalT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwalT.setGWALENT(shiroUser.getDBC());

       /* if(gwalT.getGWAL003().equals("Y")){
            this.updateGwak(gwalT);
        }*/
        gwalT.insert();
    }

    @Transactional
    @Override
    public void edit(GwalT gwalT) {
        UpdateWrapper<GwalT> gwalTTUpdateWrapper = new UpdateWrapper<>();
        gwalTTUpdateWrapper.eq("GWALENT",gwalT.getGWALENT());
        gwalTTUpdateWrapper.eq("GWAL001",gwalT.getGWAL001());
        gwalTTUpdateWrapper.eq("GWAL002",gwalT.getGWAL002());

        /*if(gwalT.getGWAL003().equals("Y")){
            this.updateGwak(gwalT);
        }*/

        gwalT.update(gwalTTUpdateWrapper);
    }

    @Transactional
    @Override
    public void delete(GwalT gwalT) {
        UpdateWrapper<GwalT> gwalTUpdateWrapper = new UpdateWrapper<>();
        gwalTUpdateWrapper.eq("GWALENT",gwalT.getGWALENT());
        gwalTUpdateWrapper.eq("GWAL001",gwalT.getGWAL001());
        gwalTUpdateWrapper.eq("GWAL002",gwalT.getGWAL002());

        gwalT.delete(gwalTUpdateWrapper);
    }

    @Transactional
    @Override
    public void delDataBatch(List<GwalT> gwalTList) {
        for(GwalT gwalT : gwalTList){
            UpdateWrapper<GwalT> gwalTUpdateWrapper = new UpdateWrapper<>();
            gwalTUpdateWrapper.eq("GWALENT",gwalT.getGWALENT());
            gwalTUpdateWrapper.eq("GWAL001",gwalT.getGWAL001());
            gwalTUpdateWrapper.eq("GWAL002",gwalT.getGWAL002());

            gwalT.delete(gwalTUpdateWrapper);
        }
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwallist(Page<Object> page, Dto dto) {
        List<Map<String, Object>> gwalDtos = this.baseMapper.listGwalPage(page, dto);
        return gwalDtos;
    }

    public void updateGwak(GwalT gwalT){
        GwakT gwakT = new GwakT();
        gwakT.setGWAK020(gwalT.getGWAL010());
        gwakT.setGWAK021(gwalT.getGWAL011());
        gwakT.setGWAK022(gwalT.getGWAL012());
        gwakT.setGWAK023(gwalT.getGWAL013());
        gwakT.setGWAK024(gwalT.getGWAL014());
        gwakT.setGWAK025(gwalT.getGWAL015());
        gwakT.setGWAK026(gwalT.getGWAL016());
        gwakT.setGWAK027(gwalT.getGWAL017());
        gwakT.setGWAK028(gwalT.getGWAL018());
        gwakT.setGWAK029(gwalT.getGWAL019());
        gwakT.setGWAK030(gwalT.getGWAL020());
        gwakT.setGWAK031(gwalT.getGWAL021());
        UpdateWrapper<GwakT> gwakTTUpdateWrapper = new UpdateWrapper<>();
        gwakTTUpdateWrapper.eq("GWAKENT",gwalT.getGWALENT());
        gwakTTUpdateWrapper.eq("GWAK001",gwalT.getGWAL001());
        gwakT.update(gwakTTUpdateWrapper);
    }
}
